#include "cDequeLikeUnorderSet.h"

#include <algorithm>
#include <deque>
#include <mutex>

namespace libcvos_vfr
{
	// 实现类的具体定义
	struct cDequeLikeUnorderSet::Impl {

		std::mutex _mtx;
		std::deque<unsigned long> _numList;
	};

	cDequeLikeUnorderSet::cDequeLikeUnorderSet():
		_pImpl(new Impl)
	{
	}

	cDequeLikeUnorderSet::~cDequeLikeUnorderSet()
	{
		delete _pImpl;
	}

	bool cDequeLikeUnorderSet::push_back(unsigned long number)
	{
		std::lock_guard<std::mutex> autoLocker(_pImpl->_mtx);
		
		if (_pImpl->_numList.end() == std::find(std::begin(_pImpl->_numList), std::end(_pImpl->_numList), number))
		{
			if (_pImpl->_numList.size() > 5)
			{
				_pImpl->_numList.pop_front();
			}
			_pImpl->_numList.push_back(number);

			return true;
		}
		else
		{
			return false;
		}
	}

	bool cDequeLikeUnorderSet::font_and_pop(unsigned long & number)
	{
		std::lock_guard<std::mutex> autoLocker(_pImpl->_mtx);

		if (_pImpl->_numList.empty())
		{
			return false;
		}

		number = _pImpl->_numList.front();
		_pImpl->_numList.pop_front();
		return true;
	}

	size_t cDequeLikeUnorderSet::size()
	{
		std::lock_guard<std::mutex> autoLocker(_pImpl->_mtx);

		return _pImpl->_numList.size();
	}

	void cDequeLikeUnorderSet::clear()
	{
		std::lock_guard<std::mutex> autoLocker(_pImpl->_mtx);
		_pImpl->_numList.clear();
	}

}
